﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
namespace Phoenix.AdminSite.Controllers
{
    public class AdminGroupController : ControllerBase
    {
        IAdminGroupService IAdminGroup = new AdminGroupService();
        IOrganizationService IOrganization = new OrganizationService();
        IIDCreaterService IIDCreater = new IDCreaterService();
        IRightsTypeService IRightsType = new RightsTypeService();
        IRightsService IRights = new RightsService();
        IAdminGroupRightService IAdminGroupRight = new AdminGroupRightService();

        public ActionResult Index(int OrgID)
        {
            ViewData["Organization"] = IOrganization.GetByOrgID(OrgID);
            ViewData["AdminGroupList"] = IAdminGroup.GetByOrgID(OrgID);
            return View();
        }

        public ActionResult Edit()
        {
            int orgID = 0;

            if (!string.IsNullOrEmpty(Request.QueryString["groupID"]))
            {
                int groupID = int.Parse(Request.QueryString["groupID"]);

                AdminGroupModel model = IAdminGroup.GetByGroupID(groupID);

                ViewData["AdminGroup"] = model;

                orgID = model.OrgID;
            }
            else
            {
                orgID = int.Parse(Request.QueryString["orgID"]);
            }

            ViewData["OrgID"] = orgID;

            return View();
        }

        public ActionResult Save(FormCollection collection)
        {
            AdminGroupModel model = new AdminGroupModel();
            model.OrgID = int.Parse(collection["hdOrgID"]);
            model.GroupName = collection["txtGroupName"];
            model.Memo = collection["txtMemo"];

            if (string.IsNullOrEmpty(collection["hdGroupID"]))
            {
                model.GroupID = IIDCreater.GetNewID("AdminGroupID", "AdminGroupID", 1000);
                IAdminGroup.AddAdminGroup(model);
            }
            else
            {
                model.GroupID = int.Parse(collection["hdGroupID"]);
                IAdminGroup.UpdateByGroupID(model);
            }
            return ShowMsg("成功", Common.ActionStatus.OK, "");
            
        }
        
        public ActionResult Delete(int groupID)
        {

            IAdminGroup.DeleteByGroupID(groupID);
            return null;
        }

        public ActionResult Rights()
        {
            int groupID=int.Parse(Request.QueryString["groupID"]);

			Dictionary<int, int> dicRights = IAdminGroupRight.GetRightIDs(groupID);

            IList<RightsAndTypeModel> rightsAndTypeList = IRightsType.GetRightsAndType();

			for (int i = 0; i < rightsAndTypeList.Count; i++)
			{
				for (int j = 0; j < rightsAndTypeList[i].Rights.Count; j++)
				{
					//存在 设为选中
					if(dicRights.ContainsKey(rightsAndTypeList[i].Rights[j].RightID))
					{
						rightsAndTypeList[i].Rights[j].Checked = true;
					}
					else
					{
						rightsAndTypeList[i].Rights[j].Checked = false;
					}
				}
			}


			

            ViewData["GroupID"] = groupID;

			ViewData["RightsAndType"] = rightsAndTypeList;

            ViewData["AdminGroupRightList"] =IAdminGroupRight.GetByGroupID(groupID);

            return View();
        }

        public ActionResult RightsSave(FormCollection collection)
        {
            IList<RightsModel> rightsList = IRights.GetAll();

            int groupID = int.Parse(collection["hdGroupID"]);

            foreach (var item in rightsList)
            {
                AdminGroupRightModel adminGroupRight = IAdminGroupRight.GetByGroupIDAndRightID(groupID, item.RightID);

                if (!string.IsNullOrEmpty(collection["chkRight_" + item.RightID]))
                {
                  
                    if (adminGroupRight == null)
                    {
                        adminGroupRight = new AdminGroupRightModel()
                        {
                            AdminGroupRightID = IIDCreater.GetNewID("AdminGroupRightID", "用户组权限ID", 1000),
                            GroupID=groupID,
                            RightID=item.RightID
                        };
                        //不存在则添加权限数据
                        IAdminGroupRight.AddAdminGroupRight(adminGroupRight);
                    }
                }
                else
                {
                    if (adminGroupRight != null)
                    {
                        IAdminGroupRight.DeleteByAdminGroupRightID(adminGroupRight.AdminGroupRightID);
                    }
                }
            }


            return ShowMsg("成功", Common.ActionStatus.OK, "");
        }

    }
}
